using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoreShop.Repository
{
    /// <summary>
    /// 说明文档：https://www.donet5.com/Doc/null/2311
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Repository<T> : SimpleClient<T> where T : class, new()
    {
        public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
        {
            if (context == null)
            {
                base.Context = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = MySqlHelper.GetMysqlString(),
                    DbType = DbType.MySql,
                    IsAutoCloseConnection = true
                });

                base.Context.Aop.OnError = (ex) =>
                {
                    //RecordHelper.Record(ex.ToString(), 3);
                };
            }
        }

        /// <summary>
        /// 批量更新实体数据
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <param name="isRemoveDataCache">是否清除缓存</param>
        /// <returns></returns>
        public SqlSugarClient GetInstance()
        {
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = MySqlHelper.GetMysqlString(),
                DbType = DbType.MySql,
                IsAutoCloseConnection = true
            });
            // 通过AOP输出SQL语句
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine($"Executing SQL: {sql}");
            };
            return db;
        }

        public void ExecuteCommand(string msql)
        {
            try
            {
                var db = GetInstance();
                db.Ado.ExecuteCommand(msql);
            }
            catch { }
        }

    }
}
